const http = require('http');
const io = require('socket.io');

let server = http.createServer();
server.listen(8080);
let wsServer = io.listen(server);//ws服务listen http服务

let socks = [];//浏览器每打开一个页面就会链接一次
wsServer.on('connection',sock=>{
  socks.push(sock);
  console.log(socks.length);
  sock.on('disconnect',()=>{
    let n = socks.indexOf(sock);
    if(n!=-1)
    {
      socks.splice(n,1);
      console.log(socks.length);
    }
  })
  sock.on('msg',function(message){
    socks.forEach(s=>{
      if(s!=sock){
        s.emit('msg',message);
      }
    })
  });
});
